﻿Imports System.Xml
Imports System.Globalization
Imports System.Threading
Public Class frmMain
    Dim xAda As Boolean = False
    Dim dokumen As New XmlDocument()
    Dim xFile As String = Application.StartupPath & "\Reminderxml.xml"
    Dim xCount As Integer
    Dim xTempDate(31) As String
    Dim xTempHour(31) As String
    Dim xTempTodo(31) As String
    Dim xBoleh As Boolean = False
    Dim xValDate As Date
    Dim xBoldDate(31) As Date
    Function cekFile() As Boolean
        If My.Computer.FileSystem.FileExists(xFile) Then
            dokumen.Load(xFile)
            Return True
        Else
            My.Computer.FileSystem.WriteAllText(xFile, "<Reminder>" & vbCrLf & "</Reminder>", False)
            Return False
        End If
        Return True
    End Function

    Function cekStartup() As Boolean
        Dim xUser As String = My.User.Name
        Dim xUser2() As String = Split(xUser, "\")

        If Dir("C:\Users\" & xUser2(1) & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ReminderFix.exe") <> "" Then

            Return True
        Else
            If MessageBox.Show("Want To Start Automatic?", "Warning", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                My.Computer.FileSystem.CopyFile(Application.StartupPath & "\Reminderxml.xml", "C:\Users\" & xUser2(1) & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ReminderXML.xml", False)
                My.Computer.FileSystem.CopyFile(Application.StartupPath & "\ReminderFix.exe", "C:\Users\" & xUser2(1) & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ReminderFix.exe", False)
            End If
            Return False
        End If
    End Function

    Sub loadTree()
        Dim xCounter As Integer = 0
        Label1.Visible = False

        xValDate = MonthCalendar1.SelectionStart
        MonthCalendar1.RemoveAllBoldedDates()
        Label1.Visible = True
        With dokumen
            .Load(xFile)
            If cekFile() Then
                If .DocumentElement.ChildNodes.Count >= 1 Then
                    Dim xRoot As New TreeNode(.DocumentElement.Name)
                    With TreeView1
                        .Nodes.Clear()
                        .Nodes.Add(xRoot)
                        For Each i As XmlNode In dokumen.DocumentElement.ChildNodes
                            Dim xSchedule As New TreeNode("Schedule : " & i.Attributes(0).Value)
                            For Each j As XmlNode In i.ChildNodes
                                If j.Name = "Date" Then
                                    If Mid(j.InnerText, 4, 2) = Format(xValDate, "MM") Then
                                        Dim xDate As String = Mid(j.InnerText, 1, 2)
                                        Dim xMonth As String = Mid(j.InnerText, 4, 2)
                                        Dim xYear As String = Mid(j.InnerText, 7, 4)
                                        Dim xtempdate2 As String
                                        If Thread.CurrentThread.CurrentCulture.ToString = "id-ID" Then
                                            Try
                                                xtempdate2 = xDate & "/" & xMonth & "/" & xYear
                                                xBoldDate(xCounter) = xtempdate2
                                            Catch ex As Exception
                                                xtempdate2 = xDate & "/" & xMonth & "/" & xYear
                                                xBoldDate(xCounter) = xtempdate2

                                            End Try
                                        End If
                                        If Thread.CurrentThread.CurrentCulture.ToString = "en-US" Then
                                            Try
                                                xtempdate2 = xMonth & "/" & xDate & "/" & xYear
                                                xBoldDate(xCounter) = xtempdate2
                                            Catch ex As Exception
                                                xtempdate2 = xMonth & "/" & xDate & "/" & xYear
                                                xBoldDate(xCounter) = xtempdate2

                                            End Try
                                        End If
                                        xTempDate(xCounter) = j.InnerText
                                        xBoleh = True
                                    End If
                                    If Format(DateTimePicker1.Value, "dd/MM/yyyy") = j.InnerText Then
                                        xRoot.Nodes.Add(xSchedule)
                                        Dim xChild As New TreeNode(j.Name & " : " & j.InnerText)
                                        xSchedule.Nodes.Add(xChild)
                                        Label1.Visible = False
                                    End If
                                Else
                                    Dim xChild2 As New TreeNode(j.Name & " : " & j.InnerText)
                                    xSchedule.Nodes.Add(xChild2)
                                End If

                                If j.Name = "Hour" And xBoleh = True Then
                                    xTempHour(xCounter) = j.InnerText
                                End If

                                If j.Name = "Todo" And xBoleh = True Then
                                    xTempTodo(xCounter) = j.InnerText
                                End If
                            Next

                            xCounter += 1
                            xBoleh = False
                        Next
                        .ExpandAll()
                    End With
                Else
                    TreeView1.Nodes.Clear()
                    Label1.Visible = True
                End If
            Else
                TreeView1.Nodes.Clear()
                Label1.Visible = True
            End If
        End With
        MonthCalendar1.BoldedDates = xBoldDate
    End Sub
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If cekFile() Then
        Else
            cekFile()
        End If
        Label3.Text = "Your Current Format Date : " & Thread.CurrentThread.CurrentCulture.ToString
        loadTree()
        cekStartup()
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmNewReminder.ShowDialog()

    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmDelete.ShowDialog()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim xtemp, xtodo As String
        If cekFile() Then
            With dokumen
                .Load(xFile)
                If .DocumentElement.ChildNodes.Count >= 1 Then
                    For Each i As XmlNode In dokumen.DocumentElement.ChildNodes
                        For Each j As XmlNode In i.ChildNodes
                            If j.Name = "Date" Then
                                If Thread.CurrentThread.CurrentCulture.ToString = "id-ID" Then
                                    If Format(Now.Date, "dd/MM/yyyy") = j.InnerText Then
                                        xAda = True
                                    End If
                                End If

                                If Thread.CurrentThread.CurrentCulture.ToString = "en-US" Then
                                    Dim xDate As String = Mid(j.InnerText, 1, 2)
                                    Dim xMonth As String = Mid(j.InnerText, 4, 2)
                                    Dim xYear As String = Mid(j.InnerText, 7, 4)
                                    Dim xtempdate2 As String
                                    xtempdate2 = (xMonth & "/" & xDate & "/" & xYear)
                                    If Format(Now.Date, "MM/dd/yyyy") = xtempdate2 Then
                                        xAda = True
                                    End If

                                End If
                                
                            End If
                            If xAda Then
                                If j.Name = "Hour" Then
                                    xtemp &= Mid(j.InnerText, 1, 5) & ","

                                End If
                                If j.Name = "Todo" Then
                                    xtodo &= j.InnerText & "~~~"
                                End If
                            End If
                        Next
                    Next
                End If
            End With
            If xAda Then
                Dim xtime As String() = Split(xtemp, ",")

                Dim xtodo1 As String() = Split(xtodo, "~~~")
                For i As Integer = 0 To UBound(xtime)
                    If Format(Now, "HH:mm") = xtime(i) And Format(Now, "ss") = "00" Then
                        My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk)
                        MessageBox.Show(xtodo1(i), "Reminder Alert")
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
        loadTree()

        For i As Integer = 0 To xTempDate.Length - 1
            If Thread.CurrentThread.CurrentCulture.ToString = "id-ID" Then
                If xTempDate(i) = Format(e.Start, "dd/MM/yyyy") Then
                    Details.ToolTipTitle = xTempDate(i) & " " & xTempHour(i) & " " & xTempTodo(i)
                End If
            End If

            If Thread.CurrentThread.CurrentCulture.ToString = "en-US" Then
                Dim xDate As String = Mid(xTempDate(i), 1, 2)
                Dim xMonth As String = Mid(xTempDate(i), 4, 2)
                Dim xYear As String = Mid(xTempDate(i), 7, 4)
                Dim xtempdate2 As String
                xtempdate2 = (xMonth & "/" & xDate & "/" & xYear)
                If xtempdate2 = Format(e.Start, "MM/dd/yyyy") Then
                    Details.ToolTipTitle = xTempDate(i) & " " & xTempHour(i) & " " & xTempTodo(i)
                End If


            End If
        Next
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Me.WindowState = FormWindowState.Normal
    End Sub

    Private Sub MinimizeOnTrayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.WindowState = FormWindowState.Minimized
    End Sub


    Private Sub ExitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem1.Click
        Application.Exit()
    End Sub

    Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem1.Click
        frmDelete.ShowDialog()
    End Sub


    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        loadTree()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        frmNewReminder.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        frmDelete.ShowDialog()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Application.Exit()
    End Sub
End Class
